package leaf.cloud.common.core.tree;

import cn.hutool.core.collection.CollUtil;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName TreeUtils
 * @Description
 * @Author YeZiHao
 * @Date 2023/4/4 18:26
 **/
public class TreeUtils {

    public static void initTree() {

    }

    public static void createTree(List<Tree> treeList, Tree treeParent) {
        if (CollUtil.isNotEmpty(treeList)) {
            treeList = treeList.stream().sorted(Comparator.comparing(Tree::getId)).collect(Collectors.toList());
            for (Tree childNode : treeList) {
                if (treeParent.getId().equals(childNode.getParentId())) {
                    List<Tree> children = treeParent.getChildren();
                    children.add(childNode);
                    childNode.setParentId(treeParent.getId());
                    createTree(treeList, childNode);
                }
            }
        }
    }
}
